package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员管理
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(Integer[] ids, HttpServletRequest request, HttpServletResponse response) {
        try {
            //System.out.println(ids);
            List<Member> members = memberService.getBusinessReport(ids);
            //获得Excel模板文件绝对路径
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
                    File.separator + "member_template.xlsx";
            //读取模板文件创建Excel表格对象
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);


            int rowNum = 1;
            XSSFRow row = sheet.createRow(1);
           /*XSSFCell cell = row.getCell(0);
            System.out.println(cell);*/
            for (Member member : members) {


                List<Order> orders = member.getOrders();
                for (Order order : orders) {
                    Setmeal setmeal = order.getSetmeal();
                    System.out.println(setmeal.getName());
                }
                List<String> setmealName = new ArrayList<>();
                List<String> checkGroupName = new ArrayList<>();
                List<String> checkItemName = new ArrayList<>();
                for (Order order : orders) {
                    Setmeal setmeal = order.getSetmeal();
                    setmealName.add(setmeal.getName());
                    List<CheckGroup> checkGroups = setmeal.getCheckGroups();
                    for (CheckGroup checkGroup : checkGroups) {
                        checkGroupName.add(checkGroup.getName());
                        List<CheckItem> checkItems = checkGroup.getCheckItems();
                        for (CheckItem checkItem : checkItems) {
                            checkItemName.add(checkItem.getName());
                        }
                    }
                }
                row.createCell(0).setCellValue(member.getName());//姓名
                row.createCell(2).setCellValue(member.getPhoneNumber());//性别
                row.createCell(1).setCellValue(member.getSex());//电话号
                row.createCell(3).setCellValue(setmealName.toString());//电话号
                row.createCell(4).setCellValue(checkGroupName.toString());//电话号
                row.createCell(5).setCellValue(checkItemName.toString());//电话号
                row = sheet.createRow(++rowNum);
            }


            //通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;fileName=member_template.xlsx");
            workbook.write(out);

            out.flush();
            out.close();
            workbook.close();

            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL, null);
        }

    }

    //根据id删除会员信息
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            memberService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);

        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        PageResult pageResult = memberService.pageQuery(queryPageBean);
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
        } catch (Exception e) {
            //新增失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Member member = memberService.findById(id);
        if (member != null) {
            Result result = new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, member);
            result.setData(member);
            return result;
        }
        return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
    }

/*

    //根据检查组合id查询对应的所有检查项id
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }



    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if(checkGroupList != null && checkGroupList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroupList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }*/
}
